package com.example.tuike.controller;

import com.example.tuike.common.SubscriptionStatus;
import com.example.tuike.dto.SubscriptionRequest;
import com.example.tuike.entity.Subscription;
import com.example.tuike.service.SubscriptionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*** @Author supersaiya
 * @Description 订阅

 * @Date 2025/1/29 20:12

 * @Version 1.0*/
@Tag(name = "订阅管理")
@RestController
@RequestMapping("/api/subscriptions")
@RequiredArgsConstructor
public class SubscriptionController {
    private final SubscriptionService subscriptionService;

    @Operation(summary = "创建新订阅")
    @PostMapping
    public ResponseEntity<Subscription> createSubscription(
            @RequestBody @Valid Subscription subscription) {
        return ResponseEntity.ok(subscriptionService.createSubscription(subscription));
    }

    @Operation(summary = "更新订阅设置")
    @PutMapping("/{subscriptionId}")
    public ResponseEntity<Subscription> updateSubscription(
            @PathVariable Integer subscriptionId,
            @RequestBody @Valid Subscription subscription) {
        subscription.setSubscriptionId(subscriptionId);
        return ResponseEntity.ok(subscriptionService.updateSubscription(subscriptionId, subscription));
    }

    @Operation(summary = "获取用户订阅列表")
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<Subscription>> getUserSubscriptions(
            @PathVariable Integer userId,
            @RequestParam(required = false) SubscriptionStatus status) {
        if (userId == null || userId <= 0) {
            throw new IllegalArgumentException("Invalid user ID");
        }
        if (status == null) {
            status = SubscriptionStatus.ACTIVE;
        }
        return ResponseEntity.ok(subscriptionService.getUserSubscriptions(userId, status));
    }

    @Operation(summary = "切换订阅状态")
    @PostMapping("/toggle")
    public ResponseEntity<Subscription> toggleSubscription(
            @RequestBody @Valid SubscriptionRequest request) {
        Subscription subscription = subscriptionService.toggleSubscription(request.getUserId(), request.getFeedId(), request.getStatus(),request.getTitle());
        return ResponseEntity.ok(subscription);
    }
    @Operation(summary = "检查用户是否已订阅某个Feed")
    @GetMapping("/check/{userId}/{feedId}")
    public ResponseEntity<Boolean> isSubscribed(
            @PathVariable Integer userId,
            @PathVariable Integer feedId) {
        return ResponseEntity.ok(subscriptionService.isSubscribed(userId, feedId));
    }

}
